using System;

namespace FluentBinding.WinForms
{
    public class DataSourceEntityAdapter<E>
        where E : class
    {
        #region Fields

        public const string DisplayMemberName = "DisplayMember";
        private readonly Func<E, string> _displayMember;

        #endregion

        #region Constructors

        public DataSourceEntityAdapter(E entity)
            : this(entity, x => x.ToString())
        {
        }

        public DataSourceEntityAdapter(E entity, Func<E, string> displayMember)
        {
            Entity = entity;
            _displayMember = displayMember;
        }

        #endregion

        #region Public properties

        public E Entity { get; private set; }

        public string DisplayMember
        {
            get { return _displayMember(Entity); }
        }

        #endregion

        #region Public methods

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (ReferenceEquals(this, obj))
                return true;

            var adapter = obj as DataSourceEntityAdapter<E>;

            if (adapter == null)
                return false;

            return Equals(Entity, adapter.Entity);
        }

        public override int GetHashCode()
        {
            return Entity == null ? GetHashCode() : Entity.GetHashCode();
        }

        #endregion
    }
}